<template>
  <div class="kkkk">
    <el-form ref="form" :model="sizeForm" label-width="120px" size="medium">
      <el-form-item label="发票类型：">
        <el-radio-group v-model="sizeForm.InvoiceType" size="medium">
          <el-radio border label="0">不开发票</el-radio>
          <el-radio border label="1">增值税普通发票</el-radio>
          <el-radio border label="2">增值税专业发票</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item size="large">
        <a :href="url" target="_blank">
          <el-button type="primary">预览报价单</el-button>
        </a>
        <el-button type="primary" @click="onSubmit(sizeForm)" :loading="loading">生成报价单</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      url: 'https://www.fdimall.com/index/api/previewPdf',
      // url: 'http://fdimall.bt/',
      // param:[],
      loading: false,
      sizeForm: {
        InvoiceType: '0',
      }
    }
  },

  methods: {
    // 预览报价单数据准备
    paramInit() {
      let user_id = this.$store.state.UserInfor.user_id
      const list = this.$store.state.QuotationData
      let param = ''
      let nums = ''
      list.forEach(element => {
        param += `${element.id},`
        nums += `${element.num},`
      })
      param = param.substr(0, param.length - 1)
      nums = nums.substr(0, nums.length - 1)
      this.url = `https://www.fdimall.com/index/api/previewPdf?user_id=${user_id}&invoice_type=${this.sizeForm.InvoiceType}&goods_id=${param}&num=${nums}`
      // this.url = `http://fdimall.bt/index/api/previewPdf?user_id=${user_id}&invoice_type=${this.sizeForm.InvoiceType}&goods_id=${param}&num=${nums}`

     console.log(this.url)
    },
    // 生成报价单
    async onSubmit(sizeForm) {
      this.loading = true
      let user_id = this.$store.state.UserInfor.user_id   // 用户id
      const list = this.$store.state.QuotationData
      let param = []                                      // 商品id
      list.forEach(element => {
        console.log(element)
        param.push({ goods_id: element.id, selling_price: element.selling_price, num: element.num })
      })
      // let payment_method = sizeForm.paymentMethod                             // 付款方式
      let invoice_type = sizeForm.InvoiceType                                    //发票类型
      console.log(user_id, param, invoice_type)
      let res = await this.$http({
        method: 'post',
        url: '/api/index/api/createQuote',
        data: {
          user_id,   //
          param,
          // payment_method,  
          invoice_type  //
        }
      })
      if (res.data.State.Code == 200) {
        this.$router.push({
          name: "MyQuotation",
        })

        this.$store.commit("leftIndex", 3)

        // this.$router.push({
        //   name: "myOrder",
        //   params: {
        //     id: "3"
        //   }
        // })
        this.loading = false
        // this.$notify({
        //   title: '成功',
        //   message: '生成报价单成功 可在个人中心我的报价单中查看...',
        //   type: 'success'
        // })
        // this.preview(user_id, invoice_type, param)
      } else {
        this.loading = true
      }
    }

  },
  created() {
    this.paramInit()
    console.log(this.$store.state.QuotationData)
  }
}
</script>
<style lang="scss" scoped>
.kkkk {
  padding-top: 20px;
  border: 1px solid #f3f3f3;
  .fapiao {
    padding-left: 27px;
  }
  .generate {
    margin-left: 1087px;
  }
}
</style>
